package com.example.mygame.my2048;

import com.example.mygame.R;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class My2048Activity extends Activity implements OnClickListener {
	private ImageButton ib_bgmusicButton;
	private ImageButton ib_revertButton;
	private ImageButton ib_restartButton;
	private My2048View my2048View;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.my2048main);
		
		ib_bgmusicButton = (ImageButton)findViewById(R.id.ib_bgmusic);
		ib_revertButton = (ImageButton)findViewById(R.id.ib_revert);
		ib_restartButton = (ImageButton)findViewById(R.id.ib_restart);
		
		my2048View = (My2048View) findViewById(R.id.my2048view);
		
		ib_bgmusicButton.setOnClickListener(this);
		ib_revertButton.setOnClickListener(this);
		ib_restartButton.setOnClickListener(this);
		ib_revertButton.setClickable(false);
		
		if (getBgMusicState()) {
			my2048View.playMusic();
			ib_bgmusicButton.setImageResource(android.R.drawable.ic_lock_silent_mode_off);
		} else {
			my2048View.stopMusic();
			ib_bgmusicButton.setImageResource(android.R.drawable.ic_lock_silent_mode);
		}
		
		((TextView)findViewById(R.id.best_score)).setText(String.valueOf(getBestScore()));
		
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_bgmusic:
			if (!getBgMusicState()) {
				my2048View.playMusic();
				ib_bgmusicButton.setImageResource(android.R.drawable.ic_lock_silent_mode_off);
			} else {
				my2048View.stopMusic();
				ib_bgmusicButton.setImageResource(android.R.drawable.ic_lock_silent_mode);
			}
			break;
		case R.id.ib_revert:
			my2048View.onRevert();
			break;
		case R.id.ib_restart:
			my2048View.onRestart();
			break;

		default:
			break;
		}
	}

	private boolean getBgMusicState() {
		SharedPreferences mySharedPreferences= getSharedPreferences("status", Activity.MODE_PRIVATE); 
		return mySharedPreferences.getBoolean("bgmusic", true); 
	}
	
	private int getBestScore() {
		SharedPreferences mySharedPreferences= getSharedPreferences("status", Activity.MODE_PRIVATE); 
		return mySharedPreferences.getInt("bestScore", 0); 
	}

}
